package com.lwh.springboot.service.impl;
import com.lwh.springboot.service.ICarService;
import com.lwh.springboot.mapper.CarMapper;
import com.lwh.springboot.pojo.entity.Car;
import com.lwh.springboot.pojo.vo.CarVO;
import com.lwh.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
// Alt+Enter:快速修复一些错误
@Service
public class CarServiceImpl implements ICarService {
    @Autowired
    private CarMapper carMapper;

    @Override
    public void deleteById(Integer id) {
        carMapper.deleteById(id);
    }

    @Override
    public void add(Car car) {
        carMapper.add(car);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        carMapper.deleteAll(ids);
    }

    public Car selectById(Integer id) {
        return carMapper.selectById(id);
    }

    @Override
    public void update(Car car) {
        carMapper.update(car);
    }

    @Override
    public PageInfo selectByPage(Integer pageNo, Integer pageSize,String carp,
                                 Integer carid,Integer rent,Integer status) {
        int offset = (pageNo - 1) * pageSize;
        //查找当前也数据
        List<CarVO> list = carMapper.selectByPage(offset, pageSize,carp,carid,rent,status);
        //查找总的数量，目的是为了算总的页数totalPage
        int totalCount = carMapper.selectTotalCount(carp,carid,rent,status);
        int totalPage = (int)Math.ceil((double) totalCount / pageSize);
        return new PageInfo<CarVO>(list, totalPage, pageNo, pageSize);
    }
}
